package fatworm.query;

import java.util.Collection;

import fatworm.storage.table.*;

public class DistinctPlan implements Plan {
	
	private Plan p;
	private Collection<String> fieldlist;
	
	public DistinctPlan(Plan p) {
		this.p = p;
		this.fieldlist = p.getSchema().fields();
	}
	
	public Scan open() {
		return new DistinctScan(p.open(), fieldlist);
	}
	
	public int blocksAccessed() {
		return p.blocksAccessed();
	}
	
	public int recordsOutput() { 
		int res = p.recordsOutput();
		for (String name : fieldlist)
			res = res /  p.distinctValues(name);
		
		return Math.max(res, 1);
	}
	
	public int distinctValues(String name) {
		return p.distinctValues(name);
	}
	
	public Schema getSchema() {
		return p.getSchema();
	}
	
}